package dnl.util.lang.reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Beans {

	/**
	 * 
	 * @param c
	 * @return
	 */
	public static String[] getBeanProperties(Class<?> c){
		Set<String> propNames = new HashSet<String>();
		List<Method> getters = Methods.getGetters(c);
		List<Method> setters = Methods.getSetters(c);
		
		Set<String> propNames1 = new HashSet<String>();
		for (Method method : getters) {
			propNames1.add(toPropName(method.getName()));
		}

		for (Method method : setters) {
			String s = toPropName(method.getName());
			if(propNames1.contains(s)){
				propNames.add(s);
			}
		}
		
		return propNames.toArray(new String[propNames.size()]);
	}
	
	
	public static Object getValue(Object obj, String propName) throws IllegalAccessException, InvocationTargetException{
		Method m = Methods.getGetter(obj.getClass(), propName);
		if(m == null){
			System.err.println();
		}
		return m.invoke(obj, (Object[])null);
	}
	
	private static String toPropName(String methodName){
		String s = methodName.substring(4);
		char c = methodName.charAt(3);
		StringBuffer sb = new StringBuffer(s);
		sb.insert(0, Character.toLowerCase(c));
		return sb.toString();
	}
	
	public static void main(String[] args) {
		String[] s = getBeanProperties(A.class);
		for (String string : s) {
			System.out.println(">> "+string);
		}
	}
	
	class A{
		private int aa;
		private int bb;
		public int getAa() {
			return aa;
		}
		public void setAa(int aa) {
			this.aa = aa;
		}
		public int getBb() {
			return bb;
		}
		public void setBb(int bb) {
			this.bb = bb;
		}
		
	}
}
